package com.base.controller;

import com.base.comm.CommContent;
import com.base.po.tbk.TbkBrandCategory;
import com.base.service.LogsService;
import com.base.service.TbkBrandCategoryService;
import com.base.service.UsersService;
import com.base.utils.IP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
@RequestMapping("/admin")
public class update_TbkBrandCategory {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private TbkBrandCategoryService tbkBrandCategoryService;
	@Autowired
	private UsersService usersService;

	// 账户信息
	@RequestMapping("/update_TbkBrandCategory")
	public ModelAndView index(HttpServletRequest request, @RequestParam(value = "id", required = true) String id)
			throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        TbkBrandCategory tbkBrandCategory = tbkBrandCategoryService.selectByPrimaryKey(id);
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
		} else {
            request.setAttribute("tbkBrandCategory", tbkBrandCategory);
			modelAndView.setViewName("jsp/update_TbkBrandCategory");
		}
		return modelAndView;
	}
	String SUCCESS = "success";

	// 更改商户信息
	@RequestMapping(value = "/updateTbkBrandCategory", method = RequestMethod.POST)
	public void updateTbkBrandCategory(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String pic, String category,
			String firstid) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");

		TbkBrandCategory tbkBrandCategory = tbkBrandCategoryService.selectByPrimaryKey(id);
		PrintWriter out = response.getWriter();

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			tbkBrandCategory.setPic(pic);
			tbkBrandCategory.setCategory(category);
			if(firstid != null && !firstid.equals("undefined")) {
				tbkBrandCategory.setTopid(firstid);
			}
			int code = tbkBrandCategoryService.updateByPrimaryKeySelective(tbkBrandCategory);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			if (code == 1) {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + category + "专场分类成功", usernamesys, true,
						IP.getIpAddr(request));
				out.print(SUCCESS);
			} else {
				logsService.insert(CommContent.TYPE_UPDATE, "修改" + category + "专场分类失败", usernamesys, true,
						IP.getIpAddr(request));
				out.print("更改失败，请您重试");
			}
		}
	}

	//删除时
	@RequestMapping(value = "/delectTbkBrandCategory", method = RequestMethod.POST)
	public void updateShopinfoesTest(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, Byte datastatus) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

		TbkBrandCategory sp = tbkBrandCategoryService.selectByPrimaryKey(id);

		PrintWriter out = response.getWriter();

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			sp.setDatastatus(datastatus);
			int code = tbkBrandCategoryService.updateByPrimaryKeySelective(sp);
			String usernamesys = (String) request.getSession().getAttribute("useracc");

				if (code == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "删除" + sp.getCategory() + "专场分类成功", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + sp.getCategory() + "专场分类失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}


		}
	}
}
